package com.bzy.common.aop.impl;

import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import com.bzy.common.aop.IBaseAspect;
import com.bzy.common.aop.ServiceLogger;
import com.bzy.common.util.local.ReflectUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


@Aspect
@Component
public class ServiceBaseAspect implements IBaseAspect {

	@Pointcut("@within(org.springframework.stereotype.Service)")
	public void pcWholeService() {
	}

	@Pointcut("@annotation(com.bzy.common.aop.ServiceLogger)")
	public void pcAnnotationService() {
	}

	@Override
	@Before("pcWholeService()")
	public void before(JoinPoint joinPoint) {
		// TODO Auto-generated method stub
	}

	@Override
	@After("pcWholeService()")
	public void after(JoinPoint joinPoint) {
		// TODO Auto-generated method stub
	}

	@Override
	@AfterReturning(value = "pcWholeService()", argNames = "returnObj", returning = "returnObj")
	public void afterReturning(JoinPoint joinPoint, Object returnObj) {
		// TODO Auto-generated method stub
	}

	@Override
	@AfterThrowing(value = "pcWholeService()", throwing = "e")
	public void afterThrowing(JoinPoint joinPoint, Throwable e) {
		// TODO Auto-generated method stub
//		LoggerUtil.error("ServiceBaseAspect.java--afterThrowing");
	}

	@Override
	@Around("pcWholeService()")
	public Object around(ProceedingJoinPoint pJoinPoint) throws Throwable {
		// TODO Auto-generated method stub
		Object o = null;
		try {
			long beginTime = System.currentTimeMillis();
			o = pJoinPoint.proceed(pJoinPoint.getArgs());
			writeLog(pJoinPoint, beginTime, System.currentTimeMillis() - beginTime);
		} catch (Throwable e) {
//			LoggerUtil.error("ServiceBaseAspect.java--around");
			e.printStackTrace();
		}
		return o;
	}

	private void writeLog(ProceedingJoinPoint pJoinPoint, long beginTime, long runTime) {
		HttpServletRequest request = null;
		ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes());
		if (servletRequestAttributes != null) {
			request = servletRequestAttributes.getRequest();
		}

		String logText = "Service-->";

		Method controllerMethod = null;
		try {
			controllerMethod = ReflectUtil.getAnnotationMethod(pJoinPoint, ServiceLogger.class);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (controllerMethod != null) {
			logText += controllerMethod.getAnnotation(ServiceLogger.class).module() + "\t";
			logText += controllerMethod.getAnnotation(ServiceLogger.class).option() + "\t";
		}

		logText += runTime + "ms\t" + pJoinPoint.getSignature().toShortString();
		if (request != null) {
			logText += "\t" + request.getPathInfo() + "\t" + request.getRemoteAddr() + "\t";
		}

//		LoggerUtil.serviceLog(logText);
	}

}
